package com.dmai.aiet.archive.repository;

import com.dmai.aiet.archive.entity.SysUser;
import org.springframework.data.domain.Pageable;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Map;

@Repository
public interface SysUserRepository extends R2dbcRepository<SysUser, Long>, ReactiveQueryByExampleExecutor<SysUser> {
    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @Query("update sys_user set name = :#{#user.name}, age = :#{#user.age} where id = :#{#user.id}")
    Mono<SysUser> updateUser(@Param("user") SysUser user);


    /**
     * 动态更新用户信息
     *
     * @param user
     * @return
     */
    @Query("UPDATE sys_user SET " +
            "name = CASE WHEN :#{#user.name} IS NULL THEN name ELSE :#{#user.name} END, " +
            "age = CASE WHEN :#{#user.age} IS NULL THEN age ELSE :#{#user.age} END " +
            "WHERE id = :#{#user.id}")
    Mono<SysUser> updateUserQuery(@Param("user") SysUser user);


    /**
     * 分页查询 foo
     *
     * @param pageable 分页信息
     * @return paged foo flux
     */
    Flux<SysUser> findAllBy(@Param("user") Map<String, Object> params, Pageable pageable);
}